package flower.com.fenzu35011111.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import flower.com.fenzu35011111.R;
import flower.com.fenzu35011111.adapter.CJ2_Adapter;
import flower.com.fenzu35011111.bean.Groups;
import flower.com.fenzu35011111.bean.Optionals;
import flower.com.fenzu35011111.db.Z_Groups_Dao;
import flower.com.fenzu35011111.db.Z_Optionals_Dao;
import flower.com.fenzu35011111.dialog.ListViewActivity;

/**
 * Created by acer on 2019/6/7.
 */

public class Fragment extends android.support.v4.app.Fragment {
    private View v;
    private TextView tv;

    //回调用来接收参数
    public static Fragment getiniturl(String position) {
        Fragment twoFragment = new Fragment();
        Bundle bundle = new Bundle();
        bundle.putString("position", position);
        twoFragment.setArguments(bundle);
        return twoFragment;
    }

    Z_Groups_Dao z_groups_dao = new Z_Groups_Dao(getActivity());
    Z_Optionals_Dao z_optionals_dao = new Z_Optionals_Dao(getActivity());
    ListView listView;
    CJ2_Adapter cj2_adapter = new CJ2_Adapter();
    String gc_id;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = View.inflate(getActivity(), R.layout.item, null);
        //获取参数
        gc_id = getArguments().getString("position");
        tv = v.findViewById(R.id.tv);
        listView = v.findViewById(R.id.listview);
        LayoutInflater inflaters = LayoutInflater.from(getActivity());
        View getlistview = inflaters.inflate(R.layout.foot, null);
        listView.addFooterView(getlistview);
        View viewById = getlistview.findViewById(R.id.tv);
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), ListViewActivity.class));
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
                Optionals optionals = query.get(position);
                if (optionals.getZhiding() == null) {


                } else if (optionals.getZhiding().equals("yes")) {
                    showDialog2(position);
                } else {
                    showDialog(position);
                }

                return true;
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {


                if (query!=null&&query.get(position) != null) {
                    Optionals optionals = query.get(position);
                    if (optionals.getZhiding() == null) {

                    } else {
                        showDialog3(position);
                    }
                } else {

                }


            }
        });

        return v;
    }

    List<Optionals> query;

    private void showDialog(final int position) {
  /*
   * 这里使用了 android.support.v7.app.AlertDialog.Builder
   * 可以直接在头部写 import android.support.v7.app.AlertDialog
   * 那么下面就可以写成 AlertDialog.Builder
   */
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("提示");
        builder.setMessage("确定将股票置顶么？");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Optionals optionals = query.get(position);
                z_optionals_dao.updata(optionals, "yes");
                gengxin();
            }
        });
        builder.show();
    }

    private void showDialog3(final int position) {
  /*
   * 这里使用了 android.support.v7.app.AlertDialog.Builder
   * 可以直接在头部写 import android.support.v7.app.AlertDialog
   * 那么下面就可以写成 AlertDialog.Builder
   */
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("提示");
        builder.setMessage("确定要刪除么？");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (query.get(position) != null) {
                    Optionals optionals = query.get(position);
                    z_optionals_dao.deleteUser(optionals);
                    gengxin();
                }

            }
        });
        builder.show();
    }

    private void showDialog2(final int position) {
  /*
   * 这里使用了 android.support.v7.app.AlertDialog.Builder
   * 可以直接在头部写 import android.support.v7.app.AlertDialog
   * 那么下面就可以写成 AlertDialog.Builder
   */
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("提示");
        builder.setMessage("确定取消股票置顶么？");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Optionals optionals = query.get(position);
                z_optionals_dao.updata(optionals, "no");
                gengxin();
            }
        });
        builder.show();
    }

    @Override
    public void onResume() {

        super.onResume();
        gengxin();
    }

    public void gengxin() {
        cj2_adapter.clear();
        List<Groups> queryall = z_groups_dao.queryall();

        Groups groups = queryall.get(Integer.valueOf(gc_id));
        tv.setText("标题" + groups.getName());
        query = z_optionals_dao.query(groups.getName());
        Log.e("1111", "xx" + groups.getName());
        listView.setAdapter(cj2_adapter);
        cj2_adapter.addItem(query);
    }
}

